// Decompiled by:       Fernflower v0.8.6
// Date:                03.08.2014 10:26:35
// Copyright:           2008-2012, Stiver
// Home page:           http://www.neshkov.com/ac_decompiler.html

package com.dynamixsoftware.printershare.mdns;

import com.dynamixsoftware.printershare.mdns.DnsPacketIn;
import com.dynamixsoftware.printershare.mdns.DnsQuestion;
import com.dynamixsoftware.printershare.mdns.DnsRecord;
import java.io.IOException;
import java.net.DatagramPacket;
import java.util.Hashtable;
import java.util.LinkedList;

public final class DnsPacketOut {

   private LinkedList<DnsRecord> authorativeAnswers;
   private byte[] data;
   int flags;
   int field_611;
   private int len;
   boolean multicast;
   Hashtable<String, Integer> names;
   int numAdditionals;
   int numAnswers;
   int numAuthorities;
   int numQuestions;
   private int off;


   public void DnsPacketOut(int var1) {
      this.DnsPacketOut(var1, true);
   }

   public void DnsPacketOut(int var1, boolean var2) {
      this.authorativeAnswers = new LinkedList();
      this.flags = var1;
      this.multicast = var2;
      this.names = new Hashtable();
      this.data = new byte[1460];
      this.off = 12;
   }

   public void addAdditionalAnswer(DnsPacketIn var1, DnsRecord var2) throws IOException {
      if(this.off < 1260 && !var2.suppressedBy(var1)) {
         this.writeRecord(var2, 0L);
         ++this.numAdditionals;
      }

   }

   public void addAnswer(DnsPacketIn var1, DnsRecord var2) throws IOException {
      if(this.numAuthorities <= 0 && this.numAdditionals <= 0) {
         if(!var2.suppressedBy(var1)) {
            this.addAnswer(var2, 0L);
         }

      } else {
         throw new IllegalStateException("Answers must be added before authorities and additionals");
      }
   }

   public void addAnswer(DnsRecord var1, long var2) throws IOException {
      if(this.numAuthorities <= 0 && this.numAdditionals <= 0) {
         if(var1 != null && (var2 == 0L || !var1.isExpired(var2))) {
            this.writeRecord(var1, var2);
            ++this.numAnswers;
         }

      } else {
         throw new IllegalStateException("Questions must be added before answers");
      }
   }

   public void addAuthorativeAnswer(DnsRecord var1) throws IOException {
      if(this.numAdditionals > 0) {
         throw new IllegalStateException("Authorative answers must be added before additional answers");
      } else {
         this.authorativeAnswers.add(var1);
         this.writeRecord(var1, 0L);
         ++this.numAuthorities;
      }
   }

   public void addQuestion(DnsQuestion var1) throws IOException {
      if(this.numAnswers <= 0 && this.numAuthorities <= 0 && this.numAdditionals <= 0) {
         ++this.numQuestions;
         this.writeQuestion(var1);
      } else {
         throw new IllegalStateException("Questions must be added before answers");
      }
   }

   public DatagramPacket createPacket() throws IOException {
      this.finish();
      return new DatagramPacket(this.data, 0, this.len);
   }

   void finish() throws IOException {
      int var1 = this.off;
      this.off = 0;
      boolean var2 = this.multicast;
      int var3 = 0;
      if(!var2) {
         var3 = this.field_611;
      }

      this.writeShort(var3);
      this.writeShort(this.flags);
      this.writeShort(this.numQuestions);
      this.writeShort(this.numAnswers);
      this.writeShort(this.numAuthorities);
      this.writeShort(this.numAdditionals);
      this.off = var1;
      this.len = this.off;
   }

   public boolean isEmpty() {
      return this.numQuestions == 0 && this.numAuthorities == 0 && this.numAdditionals == 0 && this.numAnswers == 0;
   }

   boolean isQuery() {
      return ('\u8000' & this.flags) == 0;
   }

   void writeByte(int var1) throws IOException {
      if(this.off >= this.data.length) {
         throw new IOException("buffer full");
      } else {
         byte[] var2 = this.data;
         int var3 = this.off;
         this.off = var3 + 1;
         var2[var3] = (byte)var1;
      }
   }

   void writeBytes(String var1, int var2, int var3) throws IOException {
      for(int var4 = 0; var4 < var3; ++var4) {
         this.writeByte(var1.charAt(var2 + var4));
      }

   }

   void writeBytes(byte[] var1) throws IOException {
      if(var1 != null) {
         this.writeBytes(var1, 0, var1.length);
      }

   }

   void writeBytes(byte[] var1, int var2, int var3) throws IOException {
      for(int var4 = 0; var4 < var3; ++var4) {
         this.writeByte(var1[var2 + var4]);
      }

   }

   void writeInt(int var1) throws IOException {
      this.writeShort(var1 >> 16);
      this.writeShort(var1);
   }

   void writeName(String var1) throws IOException {
      this.writeName(var1, true);
   }

   void writeName(String var1, boolean var2) throws IOException {
      while(true) {
         int var3 = var1.indexOf(46);
         if(var3 < 0) {
            var3 = var1.length();
         }

         if(var3 <= 0) {
            this.writeByte(0);
            return;
         }

         if(var2) {
            Integer var4 = (Integer)this.names.get(var1);
            if(var4 != null) {
               int var6 = var4.intValue();
               this.writeByte(192 | var6 >> 8);
               this.writeByte(var6 & 255);
               return;
            }

            this.names.put(var1, Integer.valueOf(this.off));
         }

         this.writeUTF(var1, 0, var3);
         var1 = var1.substring(var3);
         if(var1.startsWith(".")) {
            var1 = var1.substring(1);
         }
      }
   }

   void writeQuestion(DnsQuestion var1) throws IOException {
      this.writeName(var1.name);
      this.writeShort(var1.type);
      int var2 = var1.clazz;
      char var3;
      if(var1.unique && this.multicast) {
         var3 = '\u8000';
      } else {
         var3 = 0;
      }

      this.writeShort(var3 | var2);
   }

   void writeRecord(DnsRecord param1, long param2) throws IOException {
      // $FF: Couldn't be decompiled
   }

   void writeShort(int var1) throws IOException {
      this.writeByte(var1 >> 8);
      this.writeByte(var1);
   }

   void writeUTF(String var1, int var2, int var3) throws IOException {
      int var4 = 0;

      for(int var5 = 0; var5 < var3; ++var5) {
         char var8 = var1.charAt(var2 + var5);
         if(var8 >= 1 && var8 <= 127) {
            ++var4;
         } else if(var8 > 2047) {
            var4 += 3;
         } else {
            var4 += 2;
         }
      }

      this.writeByte(var4);

      for(int var6 = 0; var6 < var3; ++var6) {
         char var7 = var1.charAt(var2 + var6);
         if(var7 >= 1 && var7 <= 127) {
            this.writeByte(var7);
         } else if(var7 > 2047) {
            this.writeByte(224 | 15 & var7 >> 12);
            this.writeByte(128 | 63 & var7 >> 6);
            this.writeByte(128 | 63 & var7 >> 0);
         } else {
            this.writeByte(192 | 31 & var7 >> 6);
            this.writeByte(128 | 63 & var7 >> 0);
         }
      }

   }
}
